/*
 * @Author: rgoogle rs1766728441@gmail.com
 * @Date: 2023-04-03 15:08:43
 * @LastEditors: rgoogle rs1766728441@gmail.com
 * @LastEditTime: 2023-04-03 15:11:44
 * @FilePath: \main-page\secondterm\sort.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const path = require('path');

const fs = require('fs');



// 生成包含100个随机数的数组

let arr = [];

for (let i = 0; i < 100; i++) {

  arr.push(Math.floor(Math.random() * 100));

}



// 实现插入排序算法

for (let i = 1; i < arr.length; i++) {

  let temp = arr[i];

  let j = i - 1;

  while (j >= 0 && arr[j] > temp) {

    arr[j + 1] = arr[j];

    j--;

  }

  arr[j + 1] = temp;

}



// 获取当前脚本所在的目录

const scriptDir = path.dirname(process.argv[1]);



// 将排序后的数组以文本字符串的形式写入文本文件

fs.writeFile(path.join(scriptDir, './sortedArray.txt'), arr.join(','), (err) => {

  if (err) throw err;

  console.log('The file has been saved!');

});



console.log(arr);